home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / COMMUNIC / 0132.ZIP / CVTBIN.PAS < prev    next >
Pascal/Delphi Source File  |  1985-04-28  |  6KB  |  226 lines

  1. { Program:     CISBin
  2.   Programmer:  Ray L. McVay
  3.   Started:     6 Dec, 1984
  4.  
  5.   This is a Turbo Pascal program that converts a CompuServe
  6.   ASCII binary file to a PCDOS binary file and vice-versa.  The
  7.   format used by CIS is based on the Intel HEX file format with
  8.   the exception of the final record type being that of a DATA record
  9.   rather than the End of File record type specified by Intel.  The
  10.   following is a representative record:
  11.  
  12.   :1800A8002C0083FB00750D8BDA4B8EC326031E030083C3028BD34A8E4B
  13.   3333  3333                                              3@AD checksum
  14.   3333  333@MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMAD data
  15.   3333  3@AD record type (00 = data, 01 = (Intel) last record)
  16.   333@MMAD load address
  17.   3@AD block size (number of data elements in block)
  18.   @D start of block character
  19.  
  20.   The CIS2bin procedure is very simple minded in that it discards any
  21.   line that doesn't begin with the start of block character ':', and it
  22.   discards the Address and Record Type fields.
  23. }
  24.  
  25. program CISBin;
  26.  
  27. CONST
  28.      VERSION = '1.0';
  29.      UPDATED = '9 Dec, 1984';
  30.      CBVERSION = '1.0';
  31.      CBUPDATED = '9 Dec, 1984';
  32.      BCVERSION = '1.0';
  33.      BCUPDATED = '9 Dec, 1984';
  34.  
  35. TYPE
  36.     TWOCHAR = STRING[2];
  37.  
  38. VAR
  39.    line :STRING[255];
  40.    num :BYTE;
  41.    count,chksum,badchk,skipped :INTEGER;
  42.    choice :CHAR;
  43.    done :BOOLEAN;
  44.    inname, outname :STRING[20];
  45.    hexfile :TEXT;
  46.    binfile :FILE of BYTE;
  47.  
  48. function mkbyte(s :TWOCHAR) :INTEGER;
  49. VAR
  50.    t, r :INTEGER;
  51. begin
  52.   val(concat('$', s), t, r);
  53.   mkbyte := t;
  54. end;
  55.  
  56.  
  57. procedure outnibble(n :BYTE); {write n as 1 hex character to hexfile}
  58. begin
  59.   if n > 9 then
  60.     n := n + 7; { 10..15 -> A..F }
  61.   n := n + $30;
  62.   write(hexfile, chr(n));
  63. end;
  64.  
  65.  
  66. procedure outbyte(n :BYTE); {write n as 2 hex characters to hexfile}
  67. VAR
  68.    t :INTEGER;
  69. begin
  70.   outnibble((t SHR 4) AND 15);
  71.   outnibble(t AND 15);
  72. end;
  73.  
  74.  
  75. procedure CIS2bin;
  76. VAR
  77.    i :INTEGER;
  78. begin
  79.   clrscr;
  80.   writeln('CISBin to Binary File Converter Ver ', CBVERSION);
  81.   writeln('By Ray L. McVay - ', CBUPDATED);
  82.   writeln;
  83.   write('Input file name: ');
  84.   readln(inname);
  85.   assign(hexfile, inname);
  86.   {$I-}
  87.   reset(hexfile);
  88.   {$I+}
  89.   if ioresult <> 0 then
  90.     begin
  91.       writeln('Error: ',inname, ' not found');
  92.       halt;
  93.     end;
  94.   write('Output file name: ');
  95.   readln(outname);
  96.   assign(binfile, outname);
  97.   rewrite(binfile);
  98.   badchk := 0;
  99.   skipped := 0;
  100.   while not eof(hexfile) do
  101.     begin
  102.       readln(hexfile, line);
  103.       if line[1] = ':' then            {Intel start of block character}
  104.         begin
  105.           chksum := 0;
  106.           for i := 0 to 4 do
  107.             chksum := (chksum + mkbyte(concat(line[2 * i], line[2 * i + 1])));
  108.           count := mkbyte(concat(line[2], line[3]));
  109.           for i := 0 to count - 1 do
  110.             begin
  111.               num := mkbyte(concat(line[10 + (2 * i)], line[11 + (2 * i)]));
  112.               write(binfile, num);
  113.               chksum := (chksum + num);
  114.             end;
  115.           chksum :=
  116.             (chksum +
  117.                 mkbyte(concat(line[10 + 2 * count], line[11 + 2 * count])));
  118.           if (chksum AND $FF) <> 0 then
  119.             begin
  120.               write(' <== Bad Checksum (', chksum AND $FF, ')');
  121.               badchk := badchk + 1;
  122.             end;
  123.         end
  124.       else       {this line was not valid data because it didn't start with :}
  125.         begin
  126.           writeln('Discarding line: ', line);
  127.           skipped := skipped + 1;
  128.         end;
  129.     end;
  130.   close(hexfile);
  131.   close(binfile);
  132.   writeln('Lines discarded: ', skipped);
  133.   writeln('Blocks with bad checksums: ', badchk);
  134.   write('Press any key to continue...');
  135.   read(kbd, choice);
  136. end {CIS2Bin};
  137.  
  138.  
  139. procedure bin2CIS;
  140. CONST
  141.      FULLBLOCK = $18; {standard CIS record length}
  142. VAR
  143.    oset,i,j :INTEGER;
  144.    done :BOOLEAN;
  145.    block :ARRAY[1..FULLBLOCK] of BYTE;
  146. begin
  147.   clrscr;
  148.   writeln('Binary to CISbin File Converter Ver ', BCVERSION);
  149.   writeln('By Ray L. McVay - ', BCUPDATED);
  150.   writeln;
  151.   write('Input file name: ');
  152.   readln(inname);
  153.   assign(binfile, inname);
  154.   {$I-}
  155.   reset(binfile);
  156.   {$I+}
  157.   if ioresult <> 0 then
  158.     begin
  159.       writeln('Error: ',inname, ' not found');
  160.       halt;
  161.     end;
  162.   write('Output file name: ');
  163.   readln(outname);
  164.   assign(hexfile, outname);
  165.   rewrite(hexfile);
  166.  
  167.   done := FALSE;
  168.   oset := 0;
  169.   i := 1;
  170.   repeat
  171.     {$I-}
  172.     read(binfile, block[i]);
  173.     {$I+}
  174.     if ioresult <> 0 then
  175.       begin
  176.         done := TRUE;
  177.         i := i - 1;
  178.       end;
  179.     if (i = FULLBLOCK) or done then
  180.       begin
  181.         write(hexfile, ':');                   {start of block}
  182.         outbyte(i);                            {blocksize}
  183.         outbyte(oset SHR 8);                   {offset hi}
  184.         outbyte(oset AND $FF);                 {offset lo}
  185.         write(hexfile, '00');                  {record type = data}
  186.         chksum := i + (oset AND $FF) + (oset SHR 8);
  187.         for j := 1 to i do
  188.           begin
  189.             outbyte(block[j]);                  {data}
  190.             chksum := chksum + block[j];
  191.           end;
  192.         outbyte($FF AND (-chksum));             {checksum}
  193.         writeln(hexfile);
  194.         oset := oset + i;                       {set up for next block}
  195.         i := 0;
  196.       end;
  197.     i := i + 1;
  198.   until done;
  199.   close(binfile);
  200.   close(hexfile);
  201. end;
  202.  
  203.  
  204. begin {CISbin}
  205.   done := FALSE;
  206.   repeat
  207.     clrscr;
  208.     writeln('CIS Binary ASCII File Utility - Ver ', VERSION);
  209.     writeln('Ray McVay - ', UPDATED);
  210.     writeln;
  211.     writeln('Choose an option by number:');
  212.     writeln('1. Convert a CIS ASCII binary file to DOS binary');
  213.     writeln('2. Convert a DOS binary file to CIS ASCII binary');
  214.     writeln('3. Exit to DOS');
  215.     write('Which? ');
  216.     repeat
  217.       read(kbd, choice);
  218.     until choice in ['1'..'3'];
  219.     case choice of
  220.       '1': CIS2bin;
  221.       '2': bin2CIS;
  222.       '3': done := TRUE;
  223.     end;
  224.   until done;
  225. end.
  226.